package com.sx.gongzhong.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sx.gongzhong.BaseActivity;
import com.sx.gongzhong.R;
import com.sx.gongzhong.adapter.ReplyListAdapter;
import com.sx.gongzhong.adapter.viewholder.LegalHeadView;
import com.sx.gongzhong.model.CommentBean;
import com.sx.gongzhong.model.LegalAdviceBean;
import com.sx.gongzhong.model.MsgEvent;
import com.sx.gongzhong.net.MyRxBus;
import com.sx.gongzhong.net.ServiceFactory;
import com.sx.gongzhong.pest.LegalAdviceDetailPest;
import com.sx.gongzhong.utils.ToastUtils;
import com.sx.gongzhong.view.LegalAdviceDetailView;
import com.sx.gongzhong.widget.CustomDialog;
import com.sx.gongzhong.widget.LoadDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 法律咨询详情
 */
public class LegalAdviceDetailActivity extends BaseActivity implements LegalAdviceDetailView, View.OnClickListener {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;

    private ReplyListAdapter replyAdapter;
    private String adviceId;
    private LegalAdviceBean detailBean;
    private int page = 1, pageSize = 10;
    private List<CommentBean> mlist;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.recy_edit);
        ButterKnife.bind(this);
        setTitleName("咨询详情");
        setRightImage(R.mipmap.icon_nav_more);
        adviceId = getIntent().getStringExtra("adviceId");
        LoadDialog.show(LegalAdviceDetailActivity.this, "加载中...");
        new LegalAdviceDetailPest(this).getLegalAdviceDetail(adviceId);
    }

    @Override
    public void showError(String message) {
        LoadDialog.dismiss(LegalAdviceDetailActivity.this);
        ToastUtils.showToast(LegalAdviceDetailActivity.this, message);
        if (message.equals("获取详情失败")) {
            finish();
        }
    }

    @Override
    public void getLegalAdviceDetailSuccess(LegalAdviceBean data) {
        LoadDialog.dismiss(LegalAdviceDetailActivity.this);
        detailBean = data;
        initData(data);
        getCommentData();
    }

    @Override
    public void getReplayListSuccess(List<CommentBean> rows, int total) {
        if (page == 1) {
            mlist.clear();
            replyAdapter.setNewData(mlist);
        }
        if (rows != null && rows.size() > 0) {
            mlist.addAll(rows);
        }
        if (page == total) {
            replyAdapter.loadMoreEnd();
        } else {
            replyAdapter.loadMoreComplete();
        }
        replyAdapter.notifyDataSetChanged();
    }

    @Override
    public void deleteLegalAdviceSuccess(Boolean data) {
        LoadDialog.dismiss(LegalAdviceDetailActivity.this);
        ToastUtils.showToast(LegalAdviceDetailActivity.this, "删除成功");
        MyRxBus.get().post(new MsgEvent("LegalAdviceActivity", 1, null));
        finish();
    }

    private void initData(LegalAdviceBean item) {
        mlist = new ArrayList<>();
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                swipeRefreshLayout.setRefreshing(false);
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        replyAdapter = new ReplyListAdapter(this, R.layout.item_reply_list, mlist);
        View view = View.inflate(this, R.layout.no_comment_view, null);
        TextView tv_Hint = (TextView) view.findViewById(R.id.tv_hint);
        tv_Hint.setText("暂无答复");
        replyAdapter.setHeaderAndEmpty(true);
        replyAdapter.setEmptyView(view);
        replyAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                getCommentData();
            }
        }, recyclerView);
        View headView = LayoutInflater.from(this).inflate(R.layout.head_legal_view, null);
        replyAdapter.addHeaderView(headView);
        recyclerView.setAdapter(replyAdapter);

        LegalHeadView headViews = new LegalHeadView();
        ButterKnife.bind(headViews, headView);
        if (item.advisoryUser != null) {
            headViews.tv_Name.setText(item.advisoryUser.getName());
            RequestOptions options = new RequestOptions().centerCrop()
                    .placeholder(R.mipmap.img_head_default)
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                    .circleCrop()
                    .error(R.mipmap.img_head_default);
            Glide.with(LegalAdviceDetailActivity.this)
                    .load(ServiceFactory.getUrl(item.advisoryUser.getHeaderUrl()))
                    .apply(options)
                    .into(headViews.im_Head);
        }
        headViews.tv_Time.setText(item.advisoryTime);
        headViews.tv_Content.setText(item.content);
        headViews.tv_Type.setText(item.advisoryType != null ? "#" + item.advisoryType.name:"");
    }

    public void getCommentData() {
        new LegalAdviceDetailPest(this).getReplayList(adviceId, page, pageSize);
    }

    @OnClick({R.id.imv_Right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.imv_Right://右上角按钮显示
                if (isLogin()) {
                    if (detailBean.advisoryUser != null){
                        createPop(detailBean.advisoryUser.getId(), "");
                    }
                }
                break;
            case R.id.tv_ShutUp://整个诉求禁言
                popupWindow.dismiss();
                createDialog(2, "禁言");
                break;
            case R.id.tv_Report://举报楼主
                popupWindow.dismiss();
                createDialog(3, "举报");
                break;
            case R.id.tv_Delete://删除诉求
                popupWindow.dismiss();
                createDialog(4, "删除");
                break;
            case R.id.tv_Cancel://取消弹框
                popupWindow.dismiss();
                break;
        }
    }

    /**
     * 删除/举报/禁言
     */
    public void createDialog(final int type, String title) {
        CustomDialog.Builder builder = new CustomDialog.Builder(this);
        CustomDialog dialog = builder.setMessage("确定" + title + "吗？").
                setBackButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setConfirmButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                if (type == 2) {
                    createReport("forbiddenType", detailBean.id, "legal_advice");
                } else if (type == 3) {
                    createReport("reportType", detailBean.id, "legal_advice");
                } else if (type == 4) {
                    LoadDialog.show(LegalAdviceDetailActivity.this, "删除中...");
                    new LegalAdviceDetailPest(LegalAdviceDetailActivity.this).deleteLegalAdvice(adviceId);
                }
            }
        }).create();
        dialog.show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
